package com.flying.invest.invest.model;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.flying.common.util.Utils;
import com.flying.framework.annotation.Param;
import com.flying.invest.assets.model.Assets;

public class InvestmentProxy extends Investment{
	private static final long serialVersionUID = 1L;

	@Param(value="children",   required=false,   desc="children")
	private final List<Investment> children = Utils.newArrayList();

	public InvestmentProxy(List<Investment> children) {
		this.children.addAll(children);
	}
	
	public Investment first() {
		return this.children.get(0);
	}
	//
	@Override
	public Long getInvestId() {
		return this.first().getInvestId() * -1;
	}

	@Override
	public Long getAssetsId() {
		return this.first().getAssetsId();
	}

	@Override
	public String getCurrency() {
		return this.first().getCurrency();
	}

	@Override
	public Double getCost() {
		return this.children.stream().mapToDouble(Investment::getCost).sum();
	}

	@Override
	public Double getQuantity() {
		return this.children.stream().mapToDouble(Investment::getQuantity).sum();
	}

	@Override
	public Double getAmount() {
		return this.children.stream().mapToDouble(Investment::getAmount).sum();
	}

	@Override
	public Double getCostPrice() {
		return this.getCost() / this.getQuantity();
	}

	@Override
	public Double getPrice() {
		return this.first().getPrice();
	}

	@Override
	public Double getProfit() {
		return this.children.stream().mapToDouble(Investment::getProfit).sum();
	}

	@Override
	public Double getTax() {
		return this.children.stream().mapToDouble(Investment::getTax).sum();
	}

	@Override
	public Double getPlanMoney() {
		return this.children.stream().filter(x -> x.getPlanMoney() != null).mapToDouble(Investment::getPlanMoney).sum();
	}

	@Override
	public Double getLimitRate() {
		return this.children.stream().filter(x -> x.getLimitRate() != null).mapToDouble(Investment::getLimitRate).sum();
	}

	@Override
	public String getIncludeStrategies() {
		String tmp1 = this.children.stream().filter(x -> x.getIncludeStrategies() != null).map(x -> x.getIncludeStrategies()).collect(Collectors.joining(","));
		return Arrays.asList(tmp1.split(",")).stream().distinct().collect(Collectors.joining(","));
	}

	@Override
	public Assets getAssets() {
		return this.first().getAssets();
	}

	@Override
	public String getAssetsName() {
		return this.first().getAssetsName();
	}
	
	@Override
	public String getName() {
		return this.first().getAssetsName();
	}

	public List<Investment> getChildren() {
		return children;
	}
	
}
